import React, { useContext, useEffect, useState } from 'react'
import dayjs from 'dayjs'
import { Spin, message } from 'antd'
import CityElectricity from './CityElectricity'
import DailyTable from './DailyTable'
import CityTable from './CityTable'
import Enterprise from './Enterprise'
import IndustrysTable from './IndustrysTable'
import IndustrysEchart from './IndustrysEchart'
import Filter from './Filter'
import {
  getElectricityConsumption,
  getRegionalActivityLevel
} from '&/api/electricity'
import downloadExcelImg from '&/commonjs/downloadExcelImg'
import { LayoutContext } from '&/pages/layouts/context'
import { ActivityLevelContext } from './context'
import './index.scss'
const ActivityLevel = ({ isNew }) => {
  const { activePlan } = useContext(LayoutContext)
  const [allCity, setAllCity] = useState(null)
  const [districtData, setDistrictData] = useState(null)
  const [districtTable, setDistrictTable] = useState(null)
  const [industryData, setIndustryData] = useState(null)
  const [industryTable, setIndustryTable] = useState(null)
  const [loading, setLoading] = useState(true)
  const [cutDataType, setCutDataType] = useState(true)
  const [paramsData, setParamsData] = useState({
    time_type: 3,
    fiducial_type: 1,
    control_type: 1
  })
  const [tableQuery, setTableQuery] = useState(null)
  useEffect(() => {
    let a, b
    if (isNew) {
      a = dayjs().subtract(7, 'd').format('YYYY-MM-DD HH:00:00')
      b = dayjs().format('YYYY-MM-DD HH:00:00')
    } else {
      a = dayjs(activePlan?.start_time).format('YYYY-MM-DD HH:00:00')
      b = dayjs(activePlan?.end_time).format('YYYY-MM-DD HH:00:00')
    }
    setParamsData((state) => {
      state.start_time = a
      state.end_time = b
      getAllData()
      return {
        ...state
      }
    })
  }, [activePlan?.plan_id])
  const getParams = () => {
    let newParamsData = {}
    for (let key in paramsData) {
      if (paramsData[key] != (-1 || '-1')) {
        newParamsData[key] = paramsData[key]
      }
    }
    return newParamsData
  }
  // 全市用电量
  const getAllData = async () => {
    setLoading(true)
    try {
      let json = { ...getParams(paramsData) }
      if (!isNew) {
        json.plan_id = activePlan?.plan_id
      }
      let res = await getElectricityConsumption(json)
      let res2 = await getRegionalActivityLevel(json)
      setLoading(false)
      setAllCity(res.city_data)
      setDistrictTable(res.county_list)
      setDistrictData(res.county_data)
      setIndustryTable(res2.industry_list)
      setIndustryData(res2.industry_chart)
    } catch {
      message.error('接口错误')
      setLoading(false)
    }
  }
  const onCutType = () => {
    setCutDataType(!cutDataType)
  }
  const onDownloadExcel = () => {
    if (tableQuery) {
      const { column, dataSource } = tableQuery
      downloadExcelImg(dataSource, column, '全市数据分析')
    }
  }
  return (
    <ActivityLevelContext.Provider
      value={{
        type: paramsData.time_type,
        setTableQuery: setTableQuery,
        allCity,
        districtTable,
        districtData,
        industryTable,
        industryData
      }}
    >
      <div className="ActivityLevel">
        <Filter
          onQuery={getAllData}
          onDataType={onCutType}
          paramsData={paramsData}
          setParamsData={setParamsData}
          cutDataType={cutDataType}
          onExport={onDownloadExcel}
          loading={loading}
        />
        <Spin tip="Loading......" spinning={loading}>
          {cutDataType ? <CityElectricity /> : <CityTable />}
          <div className="bottom">
            <DailyTable />
            <div className="title">各区逐日用电量变化</div>
            <Enterprise />
          </div>
          <div className="bottom">
            <IndustrysTable />
            <div className="title">各行业逐日用电量变化</div>
            <IndustrysEchart />
          </div>
        </Spin>
      </div>
    </ActivityLevelContext.Provider>
  )
}
export default ActivityLevel
